package com.ruoyi.web.controller.his;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.his.domain.OdsHospitalData;
import com.ruoyi.his.domain.vo.OdsHospitalAllVo;
import com.ruoyi.his.domain.vo.OdsListHospitalAllVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.his.domain.OdsHospitalAll;
import com.ruoyi.his.service.IOdsHospitalAllService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 国家机构Controller
 * 
 * @author Alan
 * @date 2024-03-22
 */
@RestController
@RequestMapping("/his/allHos")
public class OdsHospitalAllController extends BaseController
{
    @Autowired
    private IOdsHospitalAllService odsHospitalAllService;

    @PostMapping("/insterHos")
    public AjaxResult insterHos(@RequestBody OdsListHospitalAllVO odsHospitalAll){
        return odsHospitalAllService.insertOdsHosAllByHosId(odsHospitalAll);
    }


    /**
     * 医院的注册业务
     * @param odsHospitalAll
     * @return
     */
    @PostMapping("/registerHospital")
    public AjaxResult register(@RequestBody OdsHospitalAll odsHospitalAll){
        if(odsHospitalAll.getReCount()==null){
            return AjaxResult.error("系统繁忙，稍后再试！");
        }
        //如果有重复的记录，则调用非选择医院的注册方式
//        if(odsHospitalAll.getReCount()>0){
//            return odsHospitalAllService.registerHospitalUnSelect(odsHospitalAll);
//        }
        //没有重复的医院名称，调用选择医院的注册方式
        return odsHospitalAllService.registerHospitalSelect(odsHospitalAll);
    }

    /**
     * 通过签名判断医院名称是否重复
     * @param signature
     * @return
     */
    @GetMapping("/checkSameName")
    public int checkSameName(@RequestParam(required = false) String signature ){
        return odsHospitalAllService.checkSameName(signature);
    }

    /**
     * 通过签名去获取医院
     * @param signature
     * @return
     */
    @GetMapping("/getHosListBySig")
    public List<OdsHospitalAll> getHosListBySig(@RequestParam(required = false) String signature ){
        return odsHospitalAllService.selectOdsHosListBySig(signature);
    }

    /**
     * 查询国家机构列表
     */
//    @PreAuthorize("@ss.hasPermi('his:all:list')")
    @GetMapping("/list")
    public TableDataInfo list(OdsHospitalAll odsHospitalAll)
    {
        startPage();
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        odsHospitalAll.setProvinceCode(user.getProvinceCode());
        odsHospitalAll.setCityCode(user.getCityCode());
        odsHospitalAll.setAreaCode(user.getAreaCode());
        odsHospitalAll.setStreetCode(user.getStreetCode());
        List<OdsHospitalAll> list = odsHospitalAllService.selectOdsHospitalAllList(odsHospitalAll);
        return getDataTable(list);
    }

    /**
     * 4.9 机构详情的 机构信息
     * @param odsHospitalAll
     * @return
     */
    @GetMapping("/listTowHos")
    public TableDataInfo listTowHos(OdsHospitalAll odsHospitalAll)
    {
        startPage();
        List<OdsHospitalAll> list = odsHospitalAllService.selectOdsHosAllInfo(odsHospitalAll);
        return getDataTable(list);
    }

    @GetMapping("/listByNum")
    public AjaxResult getByRegisterNumbers(String registerNumber,String signature){
        OdsHospitalAll byRegisterNumbers = odsHospitalAllService.getByRegisterNumberAndSignature(registerNumber,signature);
        return AjaxResult.success(byRegisterNumbers);
    }

    @GetMapping("/listByHid")
    public AjaxResult getByHospitalIdAndSignature(String hospitalId,String signature){
        OdsHospitalAll byRegisterNumbers = odsHospitalAllService.getByHospitalIdAndSignature(hospitalId,signature);
        return AjaxResult.success(byRegisterNumbers);
    }

    /**
     * 导出国家机构列表
     */
//    @PreAuthorize("@ss.hasPermi('his:all:export')")
    @Log(title = "国家机构", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OdsHospitalAll odsHospitalAll)
    {
        List<OdsHospitalAll> list = odsHospitalAllService.selectOdsHospitalAllList(odsHospitalAll);
        ExcelUtil<OdsHospitalAll> util = new ExcelUtil<OdsHospitalAll>(OdsHospitalAll.class);
        return util.exportExcel(list, "国家机构数据");
    }

    /**
     * 获取国家机构详细信息
     */
//    @PreAuthorize("@ss.hasPermi('his:all:query')")
    @GetMapping(value = "/{registerNumber}")
    public AjaxResult getInfo(@PathVariable("registerNumber") String registerNumber)
    {
        return AjaxResult.success(odsHospitalAllService.selectRegisterNumber(registerNumber));
    }

    /**
     * 新增国家机构
     */
//    @PreAuthorize("@ss.hasPermi('his:all:add')")
    @Log(title = "国家机构", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OdsHospitalAll odsHospitalAll)
    {
        return toAjax(odsHospitalAllService.insertOdsHospitalAll(odsHospitalAll));
    }

    /**
     * 修改国家机构
     */
//    @PreAuthorize("@ss.hasPermi('his:all:edit')")
    @Log(title = "国家机构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OdsHospitalAll odsHospitalAll)
    {
        return toAjax(odsHospitalAllService.updateOdsHospitalAll(odsHospitalAll));
    }

    /**
     * 删除国家机构
     */
//    @PreAuthorize("@ss.hasPermi('his:all:remove')")
    @Log(title = "国家机构", businessType = BusinessType.DELETE)
	@DeleteMapping("/{uniqueCodes}")
    public AjaxResult remove(@PathVariable Integer[] uniqueCodes)
    {
        return toAjax(odsHospitalAllService.deleteOdsHospitalAllByUniqueCodes(uniqueCodes));
    }


    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<OdsHospitalAllVo> util = new ExcelUtil<OdsHospitalAllVo>(OdsHospitalAllVo.class);
        List<OdsHospitalAllVo> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = odsHospitalAllService.importData(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<OdsHospitalAllVo> util = new ExcelUtil<OdsHospitalAllVo>(OdsHospitalAllVo.class);
        return util.importTemplateExcel("机构导入");
    }

    @PutMapping("/updateEnableds")
    public AjaxResult updateEnableds(@RequestBody OdsHospitalAll odsHospitalAll)
    {
        return toAjax(odsHospitalAllService.updateOdsHospitalAllUpEnableds(odsHospitalAll));
    }

}
